package cn.work.dao;

import cn.work.bean.Doctor;
import cn.work.util.DataBaseutil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DoctorDao {

    public List<Doctor> getAllDoctors() throws SQLException {
        List<Doctor> doctors = new ArrayList<>();
        String sql = "SELECT * FROM doctors WHERE status != 'deleted'";

        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                Doctor doctor = mapResultSetToDoctor(rs);
                doctors.add(doctor);
            }
        }
        return doctors;
    }

    public Doctor getDoctorById(int id) throws SQLException {
        String sql = "SELECT * FROM doctors WHERE id = ? AND status != 'deleted'";

        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return mapResultSetToDoctor(rs);
                }
            }
        }
        return null;
    }

    public boolean addDoctor(Doctor doctor) throws SQLException {
        String sql = "INSERT INTO doctors (name, position, description, avatar, status) VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, doctor.getName());
            stmt.setString(2, doctor.getPosition());
            stmt.setString(3, doctor.getDescription());
            stmt.setString(4, doctor.getAvatar());
            stmt.setString(5, doctor.getStatus());

            return stmt.executeUpdate() > 0;
        }
    }

    public boolean updateDoctor(Doctor doctor) throws SQLException {
        String sql = "UPDATE doctors SET name=?, position=?, description=?, avatar=?, status=? WHERE id=?";

        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, doctor.getName());
            stmt.setString(2, doctor.getPosition());
            stmt.setString(3, doctor.getDescription());
            stmt.setString(4, doctor.getAvatar());
            stmt.setString(5, doctor.getStatus());
            stmt.setInt(6, doctor.getId());

            return stmt.executeUpdate() > 0;
        }
    }

    public boolean deleteDoctor(int id) throws SQLException {
        String sql = "UPDATE doctors SET status='deleted' WHERE id=?";

        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            return stmt.executeUpdate() > 0;
        }
    }

    public List<Doctor> searchDoctors(String keyword) throws SQLException {
        List<Doctor> doctors = new ArrayList<>();
        String sql = "SELECT * FROM doctors WHERE status != 'deleted' AND (name LIKE ? OR position LIKE ? OR description LIKE ?)";

        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            String searchPattern = "%" + keyword + "%";
            stmt.setString(1, searchPattern);
            stmt.setString(2, searchPattern);
            stmt.setString(3, searchPattern);

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    doctors.add(mapResultSetToDoctor(rs));
                }
            }
        }
        return doctors;
    }

    private Doctor mapResultSetToDoctor(ResultSet rs) throws SQLException {
        Doctor doctor = new Doctor();
        doctor.setId(rs.getInt("id"));
        doctor.setName(rs.getString("name"));
        doctor.setPosition(rs.getString("position"));
        doctor.setDescription(rs.getString("description"));
        doctor.setAvatar(rs.getString("avatar"));
        doctor.setStatus(rs.getString("status"));
        return doctor;
    }
}